package com.lovo.roomProject.updatePwd.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoPassWordTxt;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.roomProject.frame.LoginFrame;
import com.lovo.roomProject.pojo.SystemDO;
import com.lovo.roomProject.updatePwd.service.UpdatePwdService;
import com.lovo.roomProject.updatePwd.service.impl.UpdatePwdServiceImpl;

/**
 * 宿舍管理系统
 *
 * @author 张成峰
 * @version 1.0
 * @description 修改密码面板
 * @see
 */
public class UpdatePwdPanel extends JPanel {
    /**
     * 旧密码文本框
     */
    private LovoPassWordTxt oldPwdTxt = new LovoPassWordTxt("请输入旧密码", 200, 100, this);
    /**
     * 新密码文本框
     */
    private LovoPassWordTxt newPwdTxt = new LovoPassWordTxt("请输入新密码", 200, 160, this);
    /**
     * 确认密码文本框
     */
    private LovoPassWordTxt rePwdTxt = new LovoPassWordTxt("请确认密码", 200, 220, this);
    /**
     * 登陆用户对象
     */
    private SystemDO loginUser;
    private UpdatePwdService updatePwdService = new UpdatePwdServiceImpl();
    public UpdatePwdPanel(SystemDO loginUser, JFrame jf) {
        this.loginUser = loginUser;
        this.setLayout(null);
        new LovoTitleLabel("修改密码", this);

        LovoButton updateButton = new LovoButton(300, 280, "修改", "image/update.png", this);
        updateButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (!updatePwd(loginUser)) {
                    return;
                }
                jf.dispose();
                new LoginFrame();
            }
        });
    }

    //-------------------

    /**
     * 修改密码
     *
     * @param loginUser 登陆用户对象
     * @return 验证是否成功
     */
    private boolean updatePwd(SystemDO loginUser) {
        String iptOldPwd = oldPwdTxt.getText();
        String iptNewPwd = newPwdTxt.getText();
        String iptConfirmPwd = rePwdTxt.getText();
        //验证原密码是否正确
        //验证两次密码是否一致
        try {
            updatePwdService.changePwd(loginUser,iptOldPwd,iptNewPwd,iptConfirmPwd);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,e.getMessage());
            return false;
        }
        JOptionPane.showMessageDialog(null, "密码修改成功");
        return true;
    }
}
